﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using Clientes;
using System.Collections;

namespace Clientes
{
    class Dados
    {
        OleDbConnection connection;
        Conexao conn = new Conexao();
        public Dados()
        {
            try
            {
                connection = conn.getConexao();

            }

            catch (OleDbException ex)
            {
                
                Console.WriteLine(ex);
            }

            
        }

        public Boolean insereDados(ArrayList dadosCliente) 
        {
            try
            {
                conn.openConexao();
                string insertCommand = "insert into clientes values ("+ dadosCliente[0] + ",'" +
                                                                        dadosCliente[1] + "','" +
                                                                        dadosCliente[2] + "','" +
                                                                        dadosCliente[3] + "','" +
                                                                        dadosCliente[4] + "')";
                OleDbCommand command = new OleDbCommand(insertCommand, connection);
                command.ExecuteNonQuery();

                return true;
            }

            catch (OleDbException ex)
            {
                Console.WriteLine(ex);

                return false;
            }
            finally
            {
                conn.closeConexao();
            }        
        }

        public DataTable selecionaDados() 
        {
            DataTable cliente = new DataTable();

            //conn.openConexao();
            string selectCommand = "select * from clientes";
            OleDbCommand command = new OleDbCommand(selectCommand, connection);
            cliente.Load(command.ExecuteReader());
            conn.closeConexao();
            return cliente;
        }

        public ArrayList findByCpf(string cpf)
        {
            ArrayList cliente = new ArrayList();

            //conn.openConexao();
            string selectCommand = "select * from clientes where cpf = '" + cpf + "'";
            OleDbCommand command = new OleDbCommand(selectCommand, connection);
            OleDbDataReader reader = command.ExecuteReader();



            while (reader.Read())
            {
                cliente.Add(reader.GetInt32(0));
                cliente.Add(reader.GetString(1));
                cliente.Add(reader.GetString(2));
                cliente.Add(reader.GetString(3));
                cliente.Add(reader.GetString(4));
            }

            conn.closeConexao();
            return cliente;
        }
    }
}
